import re
import datetime

class FeeCalculator:
    LOG_ENABLED = 0  # 0不输出日志，1输出日志
    RATE_TYPES = {

        '房产': {
            'ranges': [
                (0, 100, 0.005),
                (100, 1000, 0.0025),
                (1000, 2000, 0.0015),
                (2000, 5000, 0.0008),
                (5000, 8000, 0.0004),
                (8000, 10000, 0.0002),
                (10000, float('inf'), 0.0001)
            ]
        },
        '土地': {
            'ranges': [
                (0, 100, 0.004),
                (100, 200, 0.003),
                (200, 1000, 0.002),
                (1000, 2000, 0.0015),
                (2000, 5000, 0.0008),
                (5000, 10000, 0.0004),
                (10000, float('inf'), 0.0001)
            ]
        },
        '资产': {
            'ranges': [
                (0, 100, 0.011),
                (100, 1000, 0.0045),
                (1000, 5000, 0.0012),
                (5000, 10000, 0.0008),
                (10000, float('inf'), 0.00015)
            ]
        }
    }

    @staticmethod
    def log_action(action):
        if FeeCalculator.LOG_ENABLED:
            now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
            print(f"[{now}] {action}")

    @classmethod
    def calculate(cls, amount, rate_type):
        cls.log_action(f"开始计算 {rate_type} 评估费，金额为 {amount}")
        amount = float(amount)
        total = 0.0
        remaining = amount
        
        for min_val, max_val, rate in cls.RATE_TYPES[rate_type]['ranges']:
            if remaining <= 0:
                break
            
            range_amount = min(remaining, max_val - min_val) if max_val != float('inf') else remaining
            total += range_amount * rate
            remaining -= range_amount
            
        cls.log_action(f"完成 {rate_type} 评估费计算，结果为 {round(total, 4)}")
        return round(total, 4)